package com.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.module.Result;

public class IndexController  extends Controller {  
	public final Logger logger = Logger.getLogger(IndexController.class);

    public void index() {  
        renderJsp("index.jsp");  
    }
    @ActionKey("poi_text")
    public void poiText() {
    	Result rs = new Result();
    	rs.setStatus(false);
    	String msg = "生成成功";
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 
    	Date date = new Date();
    	String downFileName = sdf.format(date)+ ".pptx";
		String base = getRequest().getSession().getServletContext().getRealPath("/"); 
		String file = base+"file/temp.pptx";
		String destFile = base+"download/" + downFileName;
		logger.info("模板路径： "+file);
		logger.info("生成路径： "+destFile);
    	XMLSlideShow pptx;
    	try{
	    	pptx = new XMLSlideShow(new FileInputStream(file));
	    	//ppt中的第一页
	    	XSLFSlide slide = pptx.getSlides().get(0);
	    	HashMap<String, String> map = new HashMap<String, String>();
	    	map.put("{%1$1s}", "紫烟");
	    	map.put("{%1$2s}", "瀑布");
	    	map.put("{%1$3s}", "三");
	    	map.put("{%1$4s}", "银河");
	    	//遍历当前页面图形Shape，获取每一张幻灯片中的文本框XSLFTextShape
			for (XSLFShape shape : slide.getShapes()) {
				if (shape instanceof XSLFTextShape) {
					XSLFTextShape textShape = (XSLFTextShape) shape;
					//获取文本框中的段落
					List<XSLFTextParagraph> paragraphs = textShape.getTextParagraphs();
					for(XSLFTextParagraph paragraph : paragraphs){
						//获取段落中的文本域
						for(XSLFTextRun textrun :  paragraph.getTextRuns()){
							  String runText = textrun.getRawText();
							  boolean contains = map.containsKey(runText);    //判断是否包含指定的键值
							  if (contains) {         //如果条件为真
								  textrun.setText((String) map.get(runText));
							  }else{
								  logger.info("runText："+runText);
							  }
						}
					}
				}
			}
			// 保存文件
			OutputStream out = new FileOutputStream(destFile);
			pptx.write(out);
			out.close();
			
    	}catch (Exception e){
			msg = e.toString();
		}finally{
	    	
	    	File downFile=new File(destFile);
			if(downFile.exists()){  
				msg = downFileName;
				rs.setStatus(true);
	        }else{
	        	msg = "生成文件不存在";
	        }  
	        rs.setMsg(msg);
	        renderJson(rs);
		}
    	
    }
}
